<?php
/**
 * 评论模型层
 * 
 * @copyright 	Copyright (c) 2011 thinkapk.com All rights reserved.
 * @package 	ThinkAPK_Model
 * @link 		http://www.thinkapk.com/
 * @author 		richie <richbossman@163.com>
 * @version 	$Id: CommentModel.class.php 7600 2011-06-03 03:36:36Z yemusheng $
 * 
 */
class CommentModel extends CommonModel {
	
	/**
	 * 取得首页使用的评论
	 *
	 * @param int $page
	 * @param int $limit
	 * @return array
	 */
	public function indexComment($page=1, $limit=4){
		$row = $this->table($this->tablePrefix.'comment AS c')->join($this->tablePrefix.'goods AS g ON g.goods_id=c.id_value')
				->field('c.comment_id,c.content,c.add_time,g.goods_name,g.goods_id,g.goods_thumb')
				->where('c.parent_id=0 AND c.status=1 AND g.is_delete=0 AND g.is_on_sale=1')
				->page($page)
				->order('c.add_time DESC')
				->limit($limit)
				->findAll();
		if($row){
			foreach ($row as $key=>$cmt){
				$row[$key]['add_time'] = date('m-d', $cmt['add_time']);
				$row[$key]['thumb'] = $this->getImagePath($cmt['goods_thumb']);
				$row[$key]['url'] = U('mall://Product/index', array('id'=>$cmt['goods_id']));
				$row[$key]['title'] = sub_str($cmt['goods_name'], 13);
				$row[$key]['content'] = sub_str(strip_tags($cmt['content']), 45);
				unset($row[$key]['goods_thumb']);
			}
		}
		return $row;
	}
	
	/*
	 * 取咨询列表
	 * @ param int $goods_id	商品ID
	 * @ param int $type		咨询类型
	*/
	public function getComments($goods_id,$type){
		
		$where="id_value={$goods_id} and status=1 and comment_type={$type} and parent_id=0";
		$total=$this->where($where)->count();
		if($total){
			import('ORG.Util.Page');
			$PC=new Page($total,5);
			$page_bar=$PC->showAjax('ShiHui.commentPage');
			$comments=$this->field('comment_id,id_value,user_name,content,add_time')->where($where)->order('comment_id desc')->limit("{$PC->firstRow},{$PC->listRows}")->select();
			$arr = array ();
			$ids = '';
			foreach($comments as $cmt) {
				$ids .= $ids ? ",{$cmt['comment_id']}" : $cmt['comment_id'];
				$arr [$cmt['comment_id']] ['id'] = $cmt ['comment_id'];
				$arr [$cmt['comment_id']] ['username'] = $cmt ['user_name'];
				$arr [$cmt['comment_id']] ['content'] = str_replace ( '\r\n', '，', htmlspecialchars ( $cmt ['content'] ) );
				$arr [$cmt['comment_id']] ['content'] = str_replace ( '\n', '，', $arr [$cmt ['comment_id']] ['content'] );
				$arr [$cmt['comment_id']] ['add_time'] = date('Y-m-d H:i:s',$cmt ['add_time']);
			}
			/* 取得已有回复的评论 */
			if ($ids) {
				$res = $this->field('user_name,content,parent_id,add_time')->where("parent_id in ({$ids})")->select();
				foreach($res as $re) {
					$arr [$re['parent_id']] ['re_content'] = str_replace ( '\n', '，', htmlspecialchars ( $re['content'] ) );
					$arr [$re['parent_id']] ['re_add_time'] = date('Y-m-d H:i:s',$re['add_time']);
					$arr [$re['parent_id']] ['re_username'] = $re['user_name'];
				}
			}
			$result=array('list'=>$arr,'total'=>$total,'page_bar'=>$page_bar);
			unset($PC,$comments,$res,$arr,$page_bar,$where);
		}else{
			$result=array();
		}
		return $result;
	}

}
?>